/*:
**     edge.c - edge and hedge
*/

#include "a48.h"

using namespace A48;


Edge::Edge(Vertex *p0, Vertex *p1)
{
  h_[0].set_org(p0); h_[1].set_org(p1);
  h_[0].set_face(NULL); h_[1].set_face(NULL);;
  h_[0].set_next(NULL); h_[1].set_next(NULL);
  h_[0].set_edge(this); h_[1].set_edge(this);
}

Hedge* Edge::hedge(int i)
{
  switch (i)
  {
  case 0: return &h_[0];
  case 1: return &h_[1];
  }
  throw Error("edge n");
}

Hedge* Hedge::mate()
{
  return (this == e_->hedge(0))? e_->hedge(1): e_->hedge(0);
};

Hedge* Hedge::reuse(Vertex *v0, Vertex *v1)
{
  set_org(v0); mate()->set_org(v1);
  set_next(NULL); mate()->set_next(NULL);
  set_face(NULL); mate()->set_face(NULL);
  return this;
}

bool Hedge::is_bdry()
{
  return edge()->is_bdry();
}

int Hedge::getIndexMate()
{
  if (this->mate()->face() != NULL)
  {
    if (this->mate()->face()->hedge(0) == this->mate())
      return 0;

    if (this->mate()->face()->hedge(1) == this->mate())
      return 1;

    if (this->mate()->face()->hedge(2) == this->mate())
      return 2;
  }

  return -1;
}

